package zhtt.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhaoq on   2024/8/3 16:21
 **/
@RestController
public class MicroservicesController {

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value="/serviceA", method= RequestMethod.GET)
    public String service() {
        List<String> serviceList = client.getServices();
        List<ServiceInstance>  serviceInstanceList = client.getInstances("microservicesapplication");
        if(serviceInstanceList.size()>0){
            System.out.println(" print info service A."+serviceList);
        }
        return "app11-microservices service A";
    }

    @RequestMapping("rest2")
    private String rest2(){
        //http://localhost:8020/microservices-application/code/aaa
        ResponseEntity<String> response = restTemplate.getForEntity("http://gateway/user", String.class);
        String result = response.getBody();
        System.out.println(result);
        return "result";
    }

    @RequestMapping(value="/code/{code}", method= RequestMethod.GET)
    public String code(@PathVariable("code") String code) {
        System.out.println("app11-microservicesr返回数据："+code);
        return "app11-microservicesr返回数据："+code;
    }
}
